﻿/*  Copyright © 2012 Matthew David Elgert - mdelgert@yahoo.com
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation; either version 2.1 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */

using System;
using System.Configuration;
using System.Net.Configuration;
using System.Web;
using System.IO;
using System.Xml;
using System.Xml.Xsl;
using System.Net.Mail;
using System.Linq;

namespace BLL
{

    public static class SendMail
    {

        public static void SendLog()
        {

            Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration("SQLBackupFTP.exe");
            MailSettingsSectionGroup settings = (MailSettingsSectionGroup)config.GetSectionGroup("system.net/mailSettings");

            string toAddress = Common.GetSendEmailTo();
            string fromAddress = settings.Smtp.Network.UserName;
            string subject = Common.GetSendEmailSubject();
            string messageBody = "----- SQLBackup Log file begin -----" + "\r\n" + Common.GetLogTxt() + "\r\n" + "----- SQLBackup Log file end -----" + "\r\n" + "\r\n" + "Copyright © 2012 Matthew David Elgert";
            bool IsHtml = false;
            bool HasMailRelay = Common.GetHasSMTPRelayHost();
            bool HasMailSettings = false;

            if (fromAddress != null)
            {
                HasMailSettings = true;
            }
            else
            {
                fromAddress = Common.GetSendEmailFrom();
            }

            if (toAddress != null)
            {
                try
                {
                    Log.Msg("Send log file to " + toAddress + " begin");

                    if (toAddress != fromAddress & HasMailSettings == true)
                    {
                        MailMessage message = new MailMessage();
                        message.From = new System.Net.Mail.MailAddress(settings.Smtp.Network.UserName.ToString());
                        message.To.Add(new MailAddress(toAddress));
                        message.Subject = subject;
                        message.IsBodyHtml = IsHtml;
                        message.Body = messageBody;
                        SmtpClient client = new SmtpClient();
                        client.Send(message);
                        client.Dispose();
                        Log.Msg("Send log file to " + toAddress + " success");
                    }

                    if ((toAddress == fromAddress & HasMailRelay == true) | (HasMailRelay == true & HasMailSettings == false))
                    {
                        //Gmail server has a bug that does not allow send to address from the same address in this case send through an open mail relay
                        MailMessage message = new MailMessage();
                        message.From = new System.Net.Mail.MailAddress(fromAddress);
                        message.To.Add(new MailAddress(toAddress));
                        message.Subject = subject;
                        message.IsBodyHtml = IsHtml;
                        message.Body = messageBody;
                        SmtpClient client = new SmtpClient();
                        client.Host = ConfigurationManager.AppSettings["SMTPRelayHost"];
                        client.Port = int.Parse(ConfigurationManager.AppSettings["SMTPRelayPort"]);
                        client.EnableSsl = bool.Parse(ConfigurationManager.AppSettings["SMTPRelayEnableSsl"]);
                        client.Send(message);
                        client.Dispose();
                        Log.Msg("Send log file to " + toAddress + " success");
                    }

                    if (HasMailRelay == false & HasMailSettings == false)
                    {
                        Log.Msg("Send log file to " + toAddress + " failed please check mail settings");
                    }

                }
                catch (Exception ex)
                {
                    Log.Msg("Send log file to " + toAddress + " failed please check mail settings", true);
                    Log.Msg(ex.ToString(), true);
                }

            }

        }

    }

}
